package com.cupcorn.balapkarung.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.GameEngineConfiguration;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplayAnimatedSprite;
import com.cupcorn.lib.display.DisplayAutoParallax;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.game.GameAutoParallax;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;
import com.cupcorn.onlinescript.ObjectScoring;
import com.cupcorn.onlinescript.OnlineScript;

public class StateMainMenu extends GameState implements GameDefine
{
	static DisplaySprite	displaySprite;
	DisplayAutoParallax displayAutoParallax;
	DisplayAnimatedSprite displayAnimatedSprite;
	GameAutoParallax		parallax;
	boolean isButtonPlayPressed, isButtonAchPressed, isButtonShopPressed, isButtonHighPressed, isButtonOptionPressed, isButtonBackPressed, isButtonAboutPressed, isButtonOfflinePressed, isButtonOnlinePressed;
	boolean someAreaTouched;
	static Entity layerSubMenu;
	
	float yShouldLayerSubMenu;
	
	static float farMove, sizeW, sizeH, sizeWtitle, sizeHtitle, lineStep;
	final int animateStep = MAIN_MENU_ANIMATION_STEP;
	int animateCount;
	
	boolean ready, stable, animatingPlayButton, animatingClosePlayButton, playPressed, gotoGameplay, openGame;
	public static boolean waitForOnlineGameplay, onlineGameplayReady, notOnlineForGameplay;
	Line lineOnline, lineOffline;
	
	public StateMainMenu(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displaySprite = new DisplaySprite(engine, GAME_MAIN_MENU);
		displayAnimatedSprite = new DisplayAnimatedSprite(engine, GAME_MENU_MC);
		displayAnimatedSprite.newAnimatedSprite(0, engine.hud, false);
		displayAutoParallax = new DisplayAutoParallax(engine, GAME_AUTO_PARALAX);
		layerSubMenu = new Entity();
		
		lineOffline = new Line(GameEngine.CAMERA_WIDTH/2, GameEngine.CAMERA_HEIGHT/2, GameEngine.CAMERA_WIDTH - (GameEngine.CAMERA_WIDTH/4), GameEngine.CAMERA_HEIGHT/4, engine.getVertexBufferObjectManager());
		lineOnline = new Line(GameEngine.CAMERA_WIDTH/2, GameEngine.CAMERA_HEIGHT/2, GameEngine.CAMERA_WIDTH - (GameEngine.CAMERA_WIDTH/4), GameEngine.CAMERA_HEIGHT - GameEngine.CAMERA_HEIGHT/4, engine.getVertexBufferObjectManager());
		
		lineOffline.setColor(0, 0, 0);
		lineOnline.setColor(0, 0, 0);
		
		openGame = true;
	}
	
	public void init()
	{
		GameEngineConfiguration.enable_trace = !true;
		if(openGame)
		{
			openGame = false;
			engine.state[STATE_GAME_PLAY].state.init();
			engine.state[STATE_GAME_PLAY].state.exit();
		}
		
		engine.hud.attachChild(lineOffline);
		engine.hud.attachChild(lineOnline);
		
		for (int i = 0; i < GAME_MAIN_MENU.length; i++)
		{
			if(i == MAIN_MENU_PLAY || i == MAIN_MENU_PLAY_PRESSED || i == MAIN_MENU_PLAY_OFFLINE || i == MAIN_MENU_PLAY_OFFLINE_PRESSED || i == MAIN_MENU_PLAY_ONLINE || i == MAIN_MENU_PLAY_ONLINE_PRESSED || i == MAIN_MENU_TITLE || i == MAIN_MENU_BACK || i == MAIN_MENU_BACK_PRESSED)
			{
				displaySprite.newSprite(i, engine.hud);
			}
			else if(i == MAIN_MENU_TANAH)
			{
				displaySprite.newSprite(i, engine.scene, false);
			}
			else
			{
				displaySprite.newSprite(i, engine.hud, false);
				layerSubMenu.attachChild(displaySprite.getSprite(i));
			}
		}
		
		engine.hud.attachChild(layerSubMenu);
		
//		displayAnimatedSprite.newAnimatedSprite(0, engine.scene);
//		displayAnimatedSprite.getAnimatedSprite(0).animate(300);
		
		displaySprite.getSprite(MAIN_MENU_PLAY_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_BACK).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_BACK_PRESSED).setVisible(false);
		
		lineOffline.setAlpha(0);
		lineOnline.setAlpha(0);
		
//		parallax = displayAutoParallax.newSprite(0, engine.scene);
//		
//		parallax.addEntity(-5, displaySprite.getSprite(MAIN_MENU_TANAH));
		
		isButtonPlayPressed = false;
		isButtonAchPressed = false;
		isButtonShopPressed = false;
		isButtonHighPressed = false;
		isButtonOptionPressed = false;
		isButtonAboutPressed = false;
		
		isButtonBackPressed = false;
		isButtonOfflinePressed = false;
		isButtonOnlinePressed = false;
		
		someAreaTouched = false;
		
		yShouldLayerSubMenu = layerSubMenu.getY();
		
		layerSubMenu.setY(-GameEngine.CAMERA_HEIGHT);
		
		ready = false;
		stable = false;
		
		engine.WaitTimeCapture();
		
		farMove = GameEngine.CAMERA_HEIGHT / animateStep;
		sizeW = displaySprite.getSprite(MAIN_MENU_PLAY).getWidth() / animateStep;
		sizeH = displaySprite.getSprite(MAIN_MENU_PLAY).getHeight() / animateStep;
		
		sizeWtitle = displaySprite.getSprite(MAIN_MENU_TITLE).getWidth() / animateStep;
		sizeHtitle = displaySprite.getSprite(MAIN_MENU_TITLE).getHeight() / animateStep;
		
		displaySprite.getSprite(MAIN_MENU_HIGH_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_ACV_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_SHOP_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_OPTION_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_ABOUT_PRESSED).setVisible(false);
		
		displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE_PRESSED).setVisible(false);
		
		displaySprite.getSprite(MAIN_MENU_TITLE).setSize(0, 0);
		displaySprite.getSprite(MAIN_MENU_PLAY).setSize(0, 0);
		
		animateCount = 0;
		
		animatingPlayButton = false;
		
		playPressed = false;
		
		gotoGameplay = false;
		
		waitForOnlineGameplay = false;
		onlineGameplayReady = false;
		notOnlineForGameplay = false;
	}
	
	public void update(float deltaTime)
	{
		if(!ready)
		{
			makeitStable_and_Ready();
		}
		else if(animatingPlayButton)
		{
			if(animatePlayButton())
			{
				animatingPlayButton = false;
				playPressed = true;
			}
		}
		else if(animatingClosePlayButton)
		{
			if(animateClosePlayButton())
			{
				animatingClosePlayButton = false;
				playPressed = false;
			}
		}
		else if(playPressed)
		{
			main_menuFeedback_play();
		}
		else if(gotoGameplay)
		{
			if(goToGameplayAnimation())
			{
				removeAllObject();
				engine.nextState(GameDefine.STATE_GAME_PLAY, iDefines.MESSAGE_STATE_UPDATE);
			}
		}
		else
		{
			main_menuFeedback();
		}
		
		if(waitForOnlineGameplay)
		{
		}
		else if(onlineGameplayReady)
		{
			engine.playerSound.play(SOUND_OK);
			playPressed = false;
			gotoGameplay = true;
			animateCount = 0;
			StateGamePlay.isOnline = true;// OnlineScript.isConnect;
			StateGamePlay.isReset = true;
			engine.playerMusic.stop(0);
			StateGamePlay.mc[0].mc.setVisible(true);
			displayAnimatedSprite.getAnimatedSprite(0).detachSelf();
			onlineGameplayReady = false;
		}
		else if(notOnlineForGameplay)
		{
			notOnlineForGameplay = false;
		}
	}
	
	void makeitStable_and_Ready()
	{
		if(stable)
		{
			engine.playerMusic.play(MUSIC_MAIN_MENU);
			ready = playMainMenuAnimation();
		}
		
		if(engine.IsTimePassed(2000) && !stable)
		{
			stable = true;
		}
	}
	
	void main_menuFeedback()
	{
		if(!ready || !stable)
		{
			return;
		}
		
		if(isButtonPlayPressed)
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_PRESSED).setVisible(true);
			displaySprite.getSprite(MAIN_MENU_PLAY).setVisible(false);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_PRESSED).setVisible(false);
			displaySprite.getSprite(MAIN_MENU_PLAY).setVisible(true);
		}
		
		if(isButtonAchPressed)
		{
			displaySprite.getSprite(MAIN_MENU_ACV_PRESSED).setVisible(true);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_ACV_PRESSED).setVisible(false);
		}
		
		if(isButtonShopPressed)
		{
			displaySprite.getSprite(MAIN_MENU_SHOP_PRESSED).setVisible(true);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_SHOP_PRESSED).setVisible(false);
		}
		
		if(isButtonHighPressed)
		{
			displaySprite.getSprite(MAIN_MENU_HIGH_PRESSED).setVisible(true);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_HIGH_PRESSED).setVisible(false);
		}
		
		if(isButtonOptionPressed)
		{
			displaySprite.getSprite(MAIN_MENU_OPTION_PRESSED).setVisible(true);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_OPTION_PRESSED).setVisible(false);
		}
		
		if(isButtonAboutPressed)
		{
			displaySprite.getSprite(MAIN_MENU_ABOUT_PRESSED).setVisible(true);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_ABOUT_PRESSED).setVisible(false);
		}
		
		
		someAreaTouched = false;
	}
	
	void main_menuFeedback_play()
	{
		if(!ready || !stable)
		{
			return;
		}
		
		if(isButtonBackPressed)
		{
			displaySprite.getSprite(MAIN_MENU_BACK_PRESSED).setVisible(true);
			displaySprite.getSprite(MAIN_MENU_BACK).setVisible(false);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_BACK_PRESSED).setVisible(false);
			displaySprite.getSprite(MAIN_MENU_BACK).setVisible(true);
		}
		
		if(isButtonOfflinePressed)
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE_PRESSED).setVisible(true);
			displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setVisible(false);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE_PRESSED).setVisible(false);
			displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setVisible(true);
		}
		
		if(isButtonOnlinePressed)
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE_PRESSED).setVisible(true);
			displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setVisible(false);
		}
		else
		{
			displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE_PRESSED).setVisible(false);
			displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setVisible(true);
		}
		
			
		someAreaTouched = false;
	}
	
	boolean playMainMenuAnimation()
	{
		engine.camera.setZoomFactor(engine.camera.getZoomFactor() + (float)(1.0f/animateStep));
		layerSubMenu.setY(layerSubMenu.getY() + farMove);
		displaySprite.getSprite(MAIN_MENU_PLAY).setSize(displaySprite.getSprite(MAIN_MENU_PLAY).getWidth()+sizeW, displaySprite.getSprite(MAIN_MENU_PLAY).getHeight()+sizeH);
		displaySprite.getSprite(MAIN_MENU_TITLE).setSize(displaySprite.getSprite(MAIN_MENU_TITLE).getWidth()+sizeWtitle, displaySprite.getSprite(MAIN_MENU_TITLE).getHeight()+sizeHtitle);
		
//		lineOffline.setAlpha(animateCount/animateStep);
//		lineOnline.setAlpha(animateCount/animateStep);
		
		if(animateCount == animateStep)
		{
			StateGamePlay.mc[0].mc.setVisible(false);
			engine.hud.attachChild(displayAnimatedSprite.getAnimatedSprite(0));
			displayAnimatedSprite.getAnimatedSprite(0).animate(new long[]{200, 200, 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 200, 200, }, new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	boolean goToGameplayAnimation()
	{
		engine.camera.setZoomFactor(engine.camera.getZoomFactor() - (float)(1.0f/animateStep));
		layerSubMenu.setY(layerSubMenu.getY() + farMove);
		displaySprite.getSprite(MAIN_MENU_PLAY).setSize(0, 0);
		displaySprite.getSprite(MAIN_MENU_PLAY_PRESSED).setSize(0, 0);
		displaySprite.getSprite(MAIN_MENU_TITLE).setSize(displaySprite.getSprite(MAIN_MENU_TITLE).getWidth()-sizeWtitle, displaySprite.getSprite(MAIN_MENU_TITLE).getHeight()-sizeHtitle);
		displaySprite.getSprite(MAIN_MENU_BACK).setSize(0, 0);
		displaySprite.getSprite(MAIN_MENU_BACK_PRESSED).setSize(0, 0);
		
		displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setAlpha(0);
		displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE_PRESSED).setVisible(false);
		displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setAlpha(0);
		
		lineOffline.setAlpha(0);
		lineOnline.setAlpha(0);
		
//		lineOffline.setAlpha(animateCount/animateStep);
//		lineOnline.setAlpha(animateCount/animateStep);
		
		if(animateCount == animateStep)
		{
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	boolean animatePlayButton()
	{
		lineOffline.setAlpha((float) (animateCount/animateStep));
		lineOnline.setAlpha((float) (animateCount/animateStep));
		
		displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setAlpha((float) (animateCount/animateStep));
		displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setAlpha((float) (animateCount/animateStep));
		
		layerSubMenu.setY(layerSubMenu.getY() + farMove);
		
		if(animateCount == animateStep)
		{
			displaySprite.getSprite(MAIN_MENU_BACK).setVisible(true);
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	boolean animateClosePlayButton()
	{
		lineOffline.setAlpha(0);
		lineOnline.setAlpha(0);
		
		layerSubMenu.setY(layerSubMenu.getY() - farMove);
		
		if(animateCount == animateStep)
		{
			displaySprite.getSprite(MAIN_MENU_BACK).setVisible(false);
			displaySprite.getSprite(MAIN_MENU_BACK_PRESSED).setVisible(false);
			animateCount = animateStep;
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	public void exit()
	{	
		
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			engine.finish();
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{	
		if(touchEvent.isActionUp())
		{
			isButtonPlayPressed = false;
			isButtonAchPressed = false;
			isButtonShopPressed = false;
			isButtonHighPressed = false;
			isButtonOptionPressed = false;
			isButtonAboutPressed = false;
			
			isButtonBackPressed = false;
			isButtonOfflinePressed = false;
			isButtonOnlinePressed = false;
		}
		else if(touchEvent.isActionMove())
		{
			if(someAreaTouched)
			{
				
			}
			else
			{
				isButtonPlayPressed = false;
				isButtonAchPressed = false;
				isButtonShopPressed = false;
				isButtonHighPressed = false;
				isButtonOptionPressed = false;
				isButtonAboutPressed = false;
				
				isButtonBackPressed = false;
				isButtonOfflinePressed = false;
				isButtonOnlinePressed = false;
			}
		}
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(gotoGameplay)
		{
			return false;
		}
		if(pSceneTouchEvent.isActionUp())
		{	
			if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY)))
			{
				if(waitForOnlineGameplay)
				{
					return false;
				}
				if(!playPressed)
				{
					if(animateCount == animateStep)
					{
						engine.playerSound.play(SOUND_OK);
						animatingPlayButton = true;
						animateCount = 0;
						
						displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setVisible(true);
						displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setAlpha(0);
						
						displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setVisible(true);
						displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setAlpha(0);
					}
				}
				else
				{
					if(animateCount == animateStep)
					{
						engine.playerSound.play(SOUND_OK);
						animatingClosePlayButton = true;
						animateCount = 0;
						
						displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setVisible(false);
						displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).setAlpha(0);
						
						displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setVisible(false);
						displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).setAlpha(0);
					}
				}
				
			
//				removeAllObject();
//				engine.nextState(GameDefine.STATE_GAME_PLAY, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_ACV)))
			{
				engine.playerSound.play(SOUND_OK);
				engine.nextState(GameDefine.STATE_ACHIEVMENT, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_SHOP)))
			{
				engine.playerSound.play(SOUND_OK);
				engine.nextState(GameDefine.STATE_SHOP, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_HIGH)))
			{
				engine.playerSound.play(SOUND_OK);
				engine.nextState(GameDefine.STATE_HIGHSCORE, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_OPTION)))
			{
				engine.playerSound.play(SOUND_OK);
				engine.nextState(GameDefine.STATE_OPTION, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_ABOUT)))
			{
				engine.playerSound.play(SOUND_OK);
				engine.nextState(GameDefine.STATE_ABOUT, iDefines.MESSAGE_STATE_INIT);
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE)))
			{
				if(playPressed)
				{
					if(waitForOnlineGameplay)
					{
						return false;
					}
					if(displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE).getAlpha() >= 1)
					{
						engine.playerSound.play(SOUND_OK);
						playPressed = false;
						gotoGameplay = true;
						animateCount = 0;
						StateGamePlay.isOnline = false;
						StateGamePlay.isReset = true;
						engine.playerMusic.stop(0);
						StateGamePlay.mc[0].mc.setVisible(true);
						displayAnimatedSprite.getAnimatedSprite(0).detachSelf();
					}
				}
			}
			else if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE)))
			{
				if(playPressed)
				{
					if(waitForOnlineGameplay)
					{
						return false;
					}
					if(displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE).getAlpha() >= 1)
					{
						waitForOnlineGameplay = true;
						Utils.TRACE(" ONLINE");
						
//						engine.playerSound.play(SOUND_OK);
//						playPressed = false;
//						gotoGameplay = true;
//						animateCount = 0;
//						StateGamePlay.isOnline = true;// OnlineScript.isConnect;
//						StateGamePlay.isReset = true;
//						engine.playerMusic.stop(0);
//						StateGamePlay.mc[0].mc.setVisible(true);
//						displayAnimatedSprite.getAnimatedSprite(0).detachSelf();
						
						ObjectScoring object = new ObjectScoring(Utils.getUsername(), "", 0);
						
						OnlineScript.get_ObjectsScoring(object);
					}
				}
			}
				
		}
		if(playPressed)
		{
			if(pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionMove())
			{
				someAreaTouched = true;
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_BACK)))
				{
					isButtonBackPressed = !false;
					isButtonOfflinePressed = false;
					isButtonOnlinePressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY_OFFLINE)))
				{
					isButtonBackPressed = false;
					isButtonOfflinePressed = !false;
					isButtonOnlinePressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY_ONLINE)))
				{
					isButtonBackPressed = false;
					isButtonOfflinePressed = false;
					isButtonOnlinePressed = !false;
				}

			}
		}
		else
		{
			if(pSceneTouchEvent.isActionDown() || pSceneTouchEvent.isActionMove())
			{
				someAreaTouched = true;
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_PLAY)))
				{
					isButtonPlayPressed = !false;
					isButtonAchPressed = false;
					isButtonShopPressed = false;
					isButtonHighPressed = false;
					isButtonOptionPressed = false;
					isButtonAboutPressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_ACV)))
				{
					isButtonPlayPressed = false;
					isButtonAchPressed = !false;
					isButtonShopPressed = false;
					isButtonHighPressed = false;
					isButtonOptionPressed = false;
					isButtonAboutPressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_SHOP)))
				{
					isButtonPlayPressed = false;
					isButtonAchPressed = false;
					isButtonShopPressed = !false;
					isButtonHighPressed = false;
					isButtonOptionPressed = false;
					isButtonAboutPressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_HIGH)))
				{
					isButtonPlayPressed = false;
					isButtonAchPressed = false;
					isButtonShopPressed = false;
					isButtonHighPressed = !false;
					isButtonOptionPressed = false;
					isButtonAboutPressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_OPTION)))
				{
					isButtonPlayPressed = false;
					isButtonAchPressed = false;
					isButtonShopPressed = false;
					isButtonHighPressed = false;
					isButtonOptionPressed = !false;
					isButtonAboutPressed = false;
				}
				
				if(pTouchArea.equals(displaySprite.getSprite(MAIN_MENU_ABOUT)))
				{
					isButtonPlayPressed = false;
					isButtonAchPressed = false;
					isButtonShopPressed = false;
					isButtonHighPressed = false;
					isButtonOptionPressed = false;
					isButtonAboutPressed = !false;
				}
			}
		}
		
		
//		if(pSceneTouchEvent.isActionUp())
//		{
//			if(pTouchArea.equals(displaySprite.getSprite(GOS_BTN_RESTART)))
//			{
//				StateGamePlay.isReset = true;
//				backGamePlay();
//			}
//			if(pTouchArea.equals(displaySprite.getSprite(GOS_BTN_REPLAY)))
//			{
//				StateGamePlay.isReset = true;
//				StateGamePlay.isReplay = true;
//				backGamePlay();
//			}
//			if(pTouchArea.equals(displaySprite.getSprite(GOS_BTN_MENU)))
//			{
//				engine.finish();
//			}
//		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{	
		
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	private void removeAllObject()
	{
		EngineLock lock = engine.getEngine().getEngineLock();
		
		lock.lock();
		
		for (int i = 0; i < GAME_MAIN_MENU.length; i++)
		{
			displaySprite.getSprite(i).detachSelf();
		}
		
//		displayAnimatedSprite.getAnimatedSprite(0).detachSelf();
		layerSubMenu.detachSelf();
		
		lineOffline.detachSelf();
		lineOnline.detachSelf();
		
		lock.unlock();
	}
}
